package com.seven.springboot.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yinbenyang
 * @date 2019/1/30 11:29
 */
public class SevenSpringBootFilter implements Filter{

    private static final Logger LOGGER = LoggerFactory.getLogger(SevenSpringBootFilter.class);

    @Override
    public void init(FilterConfig config) throws ServletException {
        LOGGER.info("SevenSpringBootFilter init...");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)request;
		System.out.println("this is MyFilter,url:" + httpServletRequest.getRequestURL());
        HttpServletResponse httpServletResponse = (HttpServletResponse)response;
        httpServletResponse.setHeader("Access-Control-Allow-Origin","*");
        chain.doFilter(request, httpServletResponse);
    }

    @Override
    public void destroy() {
        LOGGER.info("SevenSpringBootFilter destory...");
    }
}
